package cn.hy.hot100;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;


// 最长连续序列
class Solution_128 {
    public int longestConsecutive(int[] nums) {
        Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet());
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            int cur = nums[i];
            // 去除前驱
            if (!set.contains(cur - 1)) {
                int c = 1;
                // 找下一个
                while (set.contains(cur + 1)) {
                    cur += 1;
                    c += 1;
                }
                ans = Math.max(ans, c);
            }
        }
        return ans;
    }
}